var $ISS=true;
Page = function(div,div2,total,action,curPage,style){
	this.container = document.getElementById(div);
	if (div2 == null){
		this.container2 = null;
	} else {
		this.container2 = document.getElementById(div2);
	}
	
	this.action = action;
	this.style = style;
	this.showNum=4;
	this.pagedisplay = 2;
	this.cur = (curPage==null || curPage=="") ? 1:parseInt(curPage);
	this.start = this.cur-this.pagedisplay;
	this.start = (this.start < 1) ? 1 : this.start;
	this.end = this.start + this.showNum -1;
	this.total = parseInt(total);
	if(this.end > this.total) {
		this.end = this.total;
		this.start=this.end-this.showNum;
		this.start = (this.start < 1) ? 1 : this.start;
	}
};

Page.prototype.show = function(){
	if (this.total<=1) {
		this.container.innerHTML = "&nbsp;";
		if (this.container2 != null) this.container2.innerHTML = "&nbsp;";
		return;	
	}
	var fncGoNext = XHR.Delegate(this,this.goNext);
	var fncGoPrevious = XHR.Delegate(this,this.goPrevious);
	this.container.innerHTML = "";
	if (this.container2 != null) this.container2.innerHTML = "";
	if(this.cur != 1) { 
		var temp3 = $C("span");
		temp3.innerHTML = " <b>&laquo;</b>&nbsp;";
		temp3.onclick = fncGoPrevious;
		temp3.style.cursor="pointer";
		
		var ctemp3 = $C("span");
		ctemp3.innerHTML = " <b>&laquo;</b>&nbsp;";
		ctemp3.onclick = fncGoPrevious;
		ctemp3.style.cursor="pointer";
		
		this.container.appendChild(temp3);
		if (this.container2 != null) this.container2.appendChild(ctemp3);
	}
	for (var i = this.start;i <= this.end; i ++){
		var temp2 = $C("span");
		if(i==this.cur){
			temp2.innerHTML = "&nbsp;&nbsp;<span style='width:10px; padding:0px 5px 0px 5px;border:1px solid #0099cc;color:#FFFFFF;font-weight:bold;background-color:#006699;'>"+i+"</span>&nbsp;&nbsp;";
		} else {
			temp2.innerHTML = "&nbsp;<span style='cursor:pointer;background-color:#E8EAF7;padding:0px 5px 0px 5px;'>"+i+"</span>&nbsp;";
		}
		temp2.onclick = XHR.Delegate(this,this.goTo,i);
		
		var ctemp2 = $C("span");
		if(i==this.cur){
			ctemp2.innerHTML = "&nbsp;&nbsp;<span style='width:10px; padding:0px 5px 0px 5px;border:1px solid #0099cc;color:#FFFFFF;font-weight:bold;background-color:#006699;'>"+i+"</span>&nbsp;&nbsp;";
			//ctemp2.innerHTML = "&nbsp;<span class='"+this.style+"'>&nbsp;<span style='border:1px solid; width:10px; padding:0px 5px 0px 5px;'>"+i+"</span>&nbsp;</span>&nbsp;";
		} else {
			//ctemp2.innerHTML = "<span style='cursor:pointer'>&nbsp;"+i+"&nbsp;</span>";
			ctemp2.innerHTML = "&nbsp;<span style='cursor:pointer;background-color:#E8EAF7;padding:0px 5px 0px 5px;'>"+i+"</span>&nbsp;";
		}
		ctemp2.onclick = XHR.Delegate(this,this.goTo,i);
		this.container.appendChild(temp2);
		if (this.container2 != null) this.container2.appendChild(ctemp2);
	}
	if(this.cur != this.total) {
		var temp = $C("span");
		temp.style.cursor="pointer";
		temp.innerHTML = " <b>&raquo;</b>&nbsp;";
		temp.onclick = fncGoNext;
		
		var ctemp = $C("span");
		ctemp.style.cursor="pointer";
		ctemp.innerHTML = " <b>&raquo;</b>&nbsp;";
		ctemp.onclick = fncGoNext;
		
		this.container.appendChild(temp);
		if (this.container2 != null) this.container2.appendChild(ctemp);
	}
};

Page.prototype.goTo = function(i){
	if ($ISS!=null && !$ISS) return;
	this.cur = i;
	if(this.cur == this.end){
		if(this.cur != this.total){
			if(this.end + this.pagedisplay < this.total) {
				this.end = this.end + this.pagedisplay;
				this.start = this.start + this.pagedisplay;
			} else {
				this.start = this.start + this.total - this.end;
				this.end = this.total;
			}	
		}
	}
	if(this.cur == this.start){
		if(this.cur != 1){
			if(this.start - this.pagedisplay > 0){
				this.end = this.end - this.pagedisplay;
				this.start = this.start - this.pagedisplay;
			} else {
				this.start = 1;
				this.end = 2 * this.pagedisplay;
			}
		}
	}
	this.show();
	this.action(i);
};

Page.prototype.goNext = function(){
	if ($ISS!=null && !$ISS) return;
	if(this.cur == this.end-1){
		if(this.cur != this.total){
			if(this.end + this.pagedisplay < this.total) {
				this.end = this.end + this.pagedisplay;
				this.start = this.start + this.pagedisplay;
			} else {
				this.start = this.start + this.total - this.end;
				this.end = this.total;
			}			
		}
	}
	if(this.cur != this.total) {
		this.cur = this.cur + 1;
		this.show();
		this.action(this.cur);
	}
};

Page.prototype.goPrevious = function(){
	if ($ISS!=null && !$ISS) return;
	if(this.cur == this.start+1){
		if (this.cur != 2){
			if(this.start - this.pagedisplay > 0){
				this.end = this.end - this.pagedisplay;
				this.start = this.start - this.pagedisplay;
			} else {
				this.start = 1;
				this.end = 2 * this.pagedisplay;
			}
		}
	}
	if(this.cur != 1) {
		this.cur = this.cur - 1;
		this.show();
		this.action(this.cur);
	}
};
